package com.zjdiepu.www.manage.api.zx;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.type.CreditPeopleType;
import com.zjdiepu.www.base.enums.type.CreditResult;
import com.zjdiepu.www.base.enums.type.TypeData;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.DateUtil;
import com.zjdiepu.www.commpent.mapper.zx.ZxCreditQueryVoMapper;
import com.zjdiepu.www.commpent.mapper.zx.ZxQueryCreditApplySubVoMapper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.zx.IZxCreditQueryService;
import com.zjdiepu.www.ibiz.zx.IZxQueryCreditApplySubService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.zx.dto.ZxCreditQuery;
import com.zjdiepu.www.model.zx.dto.ZxQueryCreditApplySub;
import com.zjdiepu.www.model.zx.ext.ZxCreditQueryExt;
import com.zjdiepu.www.model.zx.ext.ZxQueryCreditApplySubExt;

/**
 * 更换主借人<br/>
 * @author zhangpp<br/>
 * @time 2017年05月09日 15时16分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "ZxZhuJieRenGengHuanController", produces="application/json;charset=UTF-8", method = RequestMethod.POST)
@Api(tags = {"更换主借人"}, produces="application/json;charset=UTF-8")
public class ZxZhuJieRenGengHuanController extends BaseController {
    @Resource
    private IZxCreditQueryService zxCreditQueryService;
    @Resource
    private IZxQueryCreditApplySubService zxQueryCreditApplySubService;
    @Resource
    private ZxCreditQueryVoMapper zxCreditQueryVoMapper;
    @Resource
    private ZxQueryCreditApplySubVoMapper zxQueryCreditApplySubVoMapper;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;

    /**
     * @author xiongxiaotun <br/>
     * @Title: change  <br/>
     * @Description: 更换主借人 <br/>
     * @param applyId
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "更换主借人", response = Boolean.class, httpMethod="POST")
    @RequestMapping("change")
    public ApiResponse change(String applyId, String busiId, String subApplyId) {
    	if(StringUtils.isBlank(applyId)) {
    		return ApiResponse.fail(999, "该客户征信查询申请标识为空");
    	}
    	if(StringUtils.isBlank(subApplyId)) {
    		return ApiResponse.fail(999, "该客户征信查询子申请标识为空");
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	zxCreditQueryService.saveChange(currentUser, applyId, busiId, subApplyId);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: queryCreditPeoples  <br/>
     * @Description: 查询征信人员 <br/>
     * @param applyId
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "查询征信人员", response = Boolean.class, httpMethod="POST")
    @RequestMapping("queryCreditPeoples")
    public ApiResponse queryCreditPeoples(String applyId) {
    	getCurrentUser();
    	if(StringUtils.isBlank(applyId)) {
    		return ApiResponse.fail(999, "缺失该客户征信查询申请标识");
    	}
    	ZxQueryCreditApplySubExt zxQueryCreditApplySubExt = new ZxQueryCreditApplySubExt();
    	zxQueryCreditApplySubExt.setBaseWhere();
    	zxQueryCreditApplySubExt.withOutPage();
    	zxQueryCreditApplySubExt.setQueryCreditApplyId(applyId);
    	zxQueryCreditApplySubExt.setOrderSql("(CASE WHEN `CREDIT_PEOPLE_TYPE` = '" 
    			+ CreditPeopleType.ZHU_JIE_REN.getCode() + "' THEN 1 WHEN `CREDIT_PEOPLE_TYPE` = '" 
    			+ CreditPeopleType.PEI_OU.getCode() + "' THEN 2 WHEN `CREDIT_PEOPLE_TYPE` = '" 
    			+ CreditPeopleType.DAN_BAO_REN.getCode() + "' THEN 3 ELSE 4 END)");
    	List<ZxQueryCreditApplySub> zxQueryCreditApplySubs = zxQueryCreditApplySubService.selectByExt(zxQueryCreditApplySubExt);
    	zxQueryCreditApplySubVoMapper.mapper(zxQueryCreditApplySubs);
        return ApiResponse.success(zxQueryCreditApplySubs);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: changeList  <br/>
     * @Description: 更换主借人 <br/>
     * @param applyId
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "更换主借人列表", response = Boolean.class, httpMethod="POST")
    @RequestMapping("changeList")
    public ApiResponse changeList(@ModelAttribute ZxCreditQueryExt zxCreditQueryExt) {
    	SysUser currentUser = (SysUser)getCurrentUser();
    	zxCreditQueryExt.setBaseWhere();
    	zxCreditQueryExt.setBankLoan(currentUser.getLoanBank());
    	zxCreditQueryExt.setCodeUnits(currentUser.getCodeUnits());
    	Date today = DateUtil.truncAndAddDays(new Date(), 0);
    	if(zxCreditQueryExt.getEndDateEnd() != null) {
    		zxCreditQueryExt.setEndDateEnd(DateUtil.truncAndAddDays(zxCreditQueryExt.getEndDateEnd(), 1));
    	}
    	if(zxCreditQueryExt.getStartDateEnd() != null) {
    		zxCreditQueryExt.setStartDateEnd(DateUtil.truncAndAddDays(zxCreditQueryExt.getStartDateEnd(), 1));
    	}
    	Date endDateStart = zxCreditQueryExt.getEndDateStart();
    	if(endDateStart == null) {
    		zxCreditQueryExt.setEndDateStart(today);
    	}
    	zxCreditQueryExt.addCreditResult(CreditResult.FOCUS.getCode()).addCreditResult(CreditResult.NOMAL.getCode());
    	zxCreditQueryExt.addBusiStatus(BusiStatus.ZI_LIAO_DENG_JI.getCode()).addBusiStatus(BusiStatus.TUI_HUI.getCode()).addBusiStatus(BusiStatus.CHU_SHEN_DAN_CHU_SHEN_ZHONG.getCode())
    	.addBusiStatus(BusiStatus.CHU_SHEN_DAN_ZHONG_SHEN_ZHONG.getCode())
    	.addBusiStatus(BusiStatus.CHU_SHEN_DAN_CHU_SHEN_SHI_BAI.getCode()).addBusiStatus(BusiStatus.CHU_SHEN_DAN_ZHONG_SHEN_SHI_BAI.getCode())
    	.addBusiStatus(BusiStatus.ZI_SHEN_YI_SHEN_SHI_BAI.getCode()).addBusiStatus(BusiStatus.ZI_SHEN_ER_SHEN_SHI_BAI.getCode())
    	.addBusiStatus(BusiStatus.FENG_KONG_JING_LI_SHEN_HE_SHI_BAI.getCode()).addBusiStatus(BusiStatus.FENG_KONG_ZONG_JIAN_SHEN_HE_SHI_BAI.getCode());
    	zxCreditQueryExt.setCreditPeopleType(CreditPeopleType.PEI_OU.getCode());
    	zxCreditQueryExt.setTypeData(TypeData.SYSTEM.getCode());
    	PageVo page = getPage(zxCreditQueryExt);
		int records = zxCreditQueryService.countForChange(zxCreditQueryExt);
		page.setRecords(records);
		if (records > 0) {
			List<ZxCreditQuery> zxCreditQuerys = zxCreditQueryService.selectForChange(zxCreditQueryExt);
			zxCreditQueryVoMapper.mapper(zxCreditQuerys);
			page.setRows(zxCreditQuerys);
		} else {
			page.setRows(CollectionInitUtil.emptyList());
		}
        return ApiResponse.success(page);
    }
    
}
